package ua.khpi.si.kit49.diploma.db.dto;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * Class represents features of connections
 * 
 * @author netstalk3r
 * 
 */

@Entity
@Table(name = "connections_features")
public class ConnectionFeature {

	@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "connection"))
	@Id
	@GeneratedValue(generator = "generator")
	@Column(name = "id_connection", unique = true, nullable = false)
	private Integer id;

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@PrimaryKeyJoinColumn
	private Connection connection;

	@Column(name = "distance", nullable = false)
	private Integer distance;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "road_priority_id", nullable = false)
	private RoadPriority roadPriority;

	@Column(name = "flow_intensity", nullable = false)
	private Integer flowIntensity;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "road_quality_id", nullable = false)
	private RoadQuality roadQuality;

	@Column(name = "road_width", nullable = false)
	private Integer roadWidth;

	public ConnectionFeature() {
		super();
	}

	public ConnectionFeature(Connection connection, Integer distance,
			RoadPriority roadPriority, Integer flowIntensity,
			RoadQuality roadQuality, Integer roadWidth) {
		super();
		this.connection = connection;
		this.distance = distance;
		this.roadPriority = roadPriority;
		this.flowIntensity = flowIntensity;
		this.roadQuality = roadQuality;
		this.roadWidth = roadWidth;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getIdConnection() {
		return id;
	}

	public void setIdConnection(Integer idConnection) {
		this.id = idConnection;
	}

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public Integer getDistance() {
		return distance;
	}

	public void setDistance(Integer distance) {
		this.distance = distance;
	}

	public RoadPriority getRoadPriority() {
		return roadPriority;
	}

	public void setRoadPriority(RoadPriority roadPriority) {
		this.roadPriority = roadPriority;
	}

	public Integer getFlowIntensity() {
		return flowIntensity;
	}

	public void setFlowIntensity(Integer flowIntensity) {
		this.flowIntensity = flowIntensity;
	}

	public RoadQuality getRoadQuality() {
		return roadQuality;
	}

	public void setRoadQuality(RoadQuality roadQuality) {
		this.roadQuality = roadQuality;
	}

	public Integer getRoadWidth() {
		return roadWidth;
	}

	public void setRoadWidth(Integer roadWidth) {
		this.roadWidth = roadWidth;
	}

	@Override
	public String toString() {
		return "Connection feature: id - " + connection.getId()
				+ "; distance - " + distance + "; road priority - "
				+ roadPriority.getPriority() + "; Flow Intensity - "
				+ flowIntensity + "; Road Quality - "
				+ roadQuality.getQuality() + "; Road Width - " + roadWidth;
	}

	@Override
	public int hashCode() {
		int hash = 37;
		hash = hash * 13 + ((id == null) ? 0 : id.hashCode());
		hash = hash * 13 + ((distance == null) ? 0 : distance.hashCode());
		hash = hash * 13
				+ ((roadPriority == null) ? 0 : roadPriority.hashCode());
		hash = hash * 13
				+ ((flowIntensity == null) ? 0 : flowIntensity.hashCode());
		hash = hash * 13 + ((roadQuality == null) ? 0 : roadQuality.hashCode());
		hash = hash * 13 + ((roadWidth == null) ? 0 : roadWidth.hashCode());
		return hash;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof ConnectionFeature))
			return false;
		ConnectionFeature entry = (ConnectionFeature) obj;
		return id.equals(entry.getId()) && distance.equals(entry.getDistance())
				&& roadPriority.equals(entry.getRoadPriority())
				&& flowIntensity.equals(entry.getFlowIntensity())
				&& roadQuality.equals(entry.getRoadQuality())
				&& roadWidth.equals(entry.getRoadWidth());
	}

}
